import os, sys
import subprocess
import time
from subprocess import Popen
import pretty_write
import mpstat_read

class Experiment:

    def __init__(self, num_clients, time):
        self.process_list = []
        self.num_clients = num_clients
        self.time = time / 10
        self.file_size = "_small"
        self.request_rate_agg = 0
        self.user_agg = 0
        self.sys_agg = 0
        self.idle_agg = 0

    def run(self):
        for j in range(0,self.time):
            reader = mpstat_read.StatReader("pc22", 10)
            request_rate = 0
            reader.start()
            for i in range(0,self.num_clients):
                if i < 50:
                    self.process_list.append(Popen(["python", "webserver.py", "10", "%s"%self.file_size], stdout=subprocess.PIPE))
                elif i < 60:
                    self.process_list.append(Popen(["ssh", "ashitaka.cs.utexas.edu", "python", "webserver.py", "10", "%s"%self.file_size], stdout=subprocess.PIPE))
                elif i < 70:
                    self.process_list.append(Popen(["ssh", "fio.cs.utexas.edu", "python", "webserver.py", "10", "%s"%self.file_size], stdout=subprocess.PIPE))
                elif i < 80:
                    self.process_list.append(Popen(["ssh", "gina.cs.utexas.edu", "python", "webserver.py", "10", "%s"%self.file_size], stdout=subprocess.PIPE))
                elif i < 90:
                    self.process_list.append(Popen(["ssh", "gonza.cs.utexas.edu", "python", "webserver.py", "10", "%s"%self.file_size], stdout=subprocess.PIPE))
                elif i < 100:
                    self.process_list.append(Popen(["ssh", "kanta.cs.utexas.edu", "python", "webserver.py", "10", "%s"%self.file_size], stdout=subprocess.PIPE))
            for i in range(0,self.num_clients):
                self.process_list[i].wait()
                strings = self.process_list[i].stdout.readlines()
                request_rate += float(strings[0])
            self.process_list = []
            self.request_rate_agg += request_rate
            reader.end()
            (user, sys, idle) = reader.get_results()
            self.user_agg += user
            self.sys_agg += sys
            self.idle_agg += idle
            print "%d(%d): %.02f(%.02f)"%(self.num_clients, (1+j), (self.request_rate_agg / (j+1)), (request_rate))
            time.sleep(2)

    def getResult(self):
        return self.request_rate_agg / self.time

    def getStats(self):
        return (self.user_agg / self.time, self.sys_agg / self.time, self.idle_agg / self.time)

if __name__ == '__main__' :

    num_procs = [1,2,4,6,8,1216]
    load = [1,10,25,50,75,100]
    results = {}
    ret = 0
    
    for i in num_procs:
        ret = os.system('ssh root@pc22 "cd classify; ./makesys.sh %d classify && make -j" > /dev/null 2>> ./error.log'%i)
        if(ret == 0):
            print "Make with %d processors succeeded."%i
        line = []
        sp = mpstat_read.StatPrint("util%d.dat"%i)
        for j in load:
            ret = os.system('ssh pc22 "cd classify; screen -d -m ./classify"')
            if(ret == 0):
                print "Execution of classify succeeded for load %d."%j
            else:
                exit("Execution failed with load %d."%j)
            time.sleep(5)
            exp = Experiment(j, 180)
            exp.run()
            sp.print_util(j, exp.getStats())
            line.append(exp.getResult())
            ret = os.system('ssh root@pc22 "killall -9 classify"')
            if(ret == 0):
                print "Successfully killed classify for load %d."%j
            else:
                exit("Kill failed with load %d."%j)
        results[i] = line
    
    pretty = pretty_write.Pretty("classify_load.dat", load, num_procs, results)
    pretty.writeOut()
    pretty.printOut()

    

